import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/loginPage.vue')
  },

  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/homePage.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('@/components/welcomeCom.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('@/components/rolesCom.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('@/components/usersCom')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('@/components/rightsCom.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('@/components/goodCateCom.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('@/components/goodsCom.vue')
      },
      {
        path: '/goods/add',
        component: () => import('@/components/addCom.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('@/components/paramsCom.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('@/components/ordersCom.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('@/components/reportsCom.vue')

      }
    ]
  },
  {
    path: '/add',
    name: 'add',
    component: () => import('@/components/addCom.vue')
  },
  {
    path: '/',
    redirect: '/login'
  }
]

const router = new VueRouter({
  routes
})
// 配置全局路由
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
